<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .time {
            display: block;
            font-size: 20px;
            /* color: gray; */
        }
    </style>
</head>

<body>
    <h1>现在是北京时间:</h1>
    <span class="time">
    </span>
    <script>

        fn()
        var timer = setInterval(function () {
            fn()
        }, 1000)
        function fn() {
            var arr = ['日', '一', '二', '三', '四', '五', '六']
            var date = new Date()
            var year = date.getFullYear()
            var month = date.getMonth() + 1
            var days = date.getDay()
            var hours = date.getHours()
            var minute = date.getMinutes()
            var seconds = date.getSeconds()
            var week = date.getDay() * 1
            //获取当前时间
            var span = document.getElementsByClassName('time')[0]
            span.innerHTML = `${year}年${add0(month)}月${add0(days)}日 ${add0(hours)}:${add0(minute)}:${add0(seconds)} 星期${arr[week]}`
        }
        function add0(n) {
            return n > 9 ? n : '0' + n
        }

    </script>
</body>

</html>